<?php
namespace Org\Util;
class OAuthV2 {

    /**
     * @ignore
     */
    public $client_id;

    /**
     * @ignore
     */
    public $appkey='7job';
    /**
     * @ignore
     */
    public $appsecret='af3aa295-a48f-47df-b77f-0591768eedf5';
    /**
     * @ignore
     */
    public $access_token;
    /**
     * @ignore
     */
    public $refresh_token;
    /**
     * Contains the last HTTP status code returned. 
     *
     * @ignore
     */
    public $http_code;
    /**
     * Contains the last API call.
     *
     * @ignore
     */
    public $url;
    /**
     * Set up the API root URL.
     *
     * @ignore
     */
    public $host = "https://member.13980.com/";
    /**
     * Set timeout default.
     *
     * @ignore
     */
    public $timeout = 30;
    /**
     * Set connect timeout.
     *
     * @ignore
     */
    public $connecttimeout = 30;
    /**
     * Verify SSL Cert.
     *
     * @ignore
     */
    public $ssl_verifypeer = FALSE;
    /**
     * Respons format.
     *
     * @ignore
     */
    public $format = 'json';
    /**
     * Decode returned json data.
     *
     * @ignore
     */
    public $decode_json = TRUE;
    /**
     * Contains the last HTTP headers returned.
     *
     * @ignore
     */
    public $http_info;
    /**
     * Set the useragnet.
     *
     * @ignore
     */
    public $useragent = '13980 T OAuth2 v0.1';

    /**
     * print the debug info
     *
     * @ignore
     */
    public $debug = FALSE;

    /**
     * boundary of multipart
     * @ignore
     */
    public static $boundary = '';

    function __construct($key, $secret) {
        $this->appkey = $key;
        $this->appsecret = $secret;
    }


    /**
     * Make an HTTP request
     *
     * @return string API results
     * @ignore
     */
    function http($url, $method, $postfields = NULL, $headers = array()) {
        $boundary = uniqid('------------------');
        $headers[] = "Content-Type: application/x-www-form-urlencoded; boundary=" . $boundary;

        $this->http_info = array();
        $ci = curl_init();
        /* Curl settings */
        curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
        curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ci, CURLOPT_ENCODING, "");
        curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
        curl_setopt($ci, CURLOPT_HEADER, FALSE);

        switch ($method) {
            case 'POST':
                curl_setopt($ci, CURLOPT_POST, TRUE);
                if (!empty($postfields)) {
                    curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
                    $this->postdata = $postfields;
                }
                break;
            case 'DELETE':
                curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
                if (!empty($postfields)) {
                    $url = "{$url}?{$postfields}";
                }
        }

        if ( isset($this->access_token) && $this->access_token )
            $headers[] = "Authorization: OAuth2 ".$this->access_token;

        $headers[] = "API-RemoteIP: " . $_SERVER['REMOTE_ADDR'];
        curl_setopt($ci, CURLOPT_URL, $url );
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers );
        curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE );

        $response = curl_exec($ci);
        $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
        $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
        $this->url = $url;

        // if ($this->debug) {
        //     echo "=====post data======\r\n";
        //     var_dump($postfields);

        //     echo '=====info====='."\r\n";
        //     print_r( curl_getinfo($ci) );

        //     echo '=====$response====='."\r\n";
        //     print_r( $response );
        // }
        curl_close ($ci);
        return $response;
    }

    function getaccesstoken($url, $method, $params=array()){
        // $params=array();
        $params['appKey']=$this->appkey;
        $params['appSecret']=$this->appsecret;
        $params['method']='getAccessToken';

        $body = http_build_query($params);

        $headers=array();
        $headers[] = "Content-Type: application/x-www-form-urlencoded; boundary=" . $boundary;

        return $this->http($url,$method,$body,$headers);

    }

    function getvalidateuser($url, $method,$accessToken,$username,$password){
        $params=array();
        $params['appKey']=$this->appkey;
        $params['accessToken']=$accessToken;
        $params['username']=$username;
        $params['password']=$password;
        $params['method']='validateUser';

        $body = http_build_query($params);

        $headers=array();
        $headers[] = "Content-Type: application/x-www-form-urlencoded; boundary=" . $boundary;

        return $this->http($url,$method,$body,$headers);
    }

    function adduser($url, $method,$accessToken,$username,$password,$phone,$registerIp){
        $params['appKey']=$this->appkey;
        $params['accessToken']=$accessToken;
        $params['username']=$username;
        $params['password']=$password;
        $params['method']='addUser';
        $params['phone']=$phone;
        $params['registerIp']=$registerIp;


        $body = http_build_query($params);

        $headers=array();
        $headers[] = "Content-Type: application/x-www-form-urlencoded; boundary=" . $boundary;

        return $this->http($url,$method,$body,$headers);

    }

    function updateuser($url, $method,$accessToken,$openId,$fields,$password,$phone){
        $params['appKey']=$this->appkey;
        $params['accessToken']=$accessToken;
        $params['openId']=$openId;
        $params['password']=$password;
        $params['method']='updateUser';
        $params['phone']=$phone;
        $params['fields']=$fields;

        $body = http_build_query($params);

        $headers=array();
        $headers[] = "Content-Type: application/x-www-form-urlencoded; boundary=" . $boundary;

        return $this->http($url,$method,$body,$headers);
    }
}